﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PhysioAssist2
{
    public partial class TabletPacienteRegistro : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {

            if (txtObservacao.Text == "" && fileArquivo.FileName != "") return;



            //UPLOAD
            string caminhosalvar = "";
            int tipo = 0;
            try
            {
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("Uploads"));
                di.FullName.Replace(" ", "_");
                if (!di.Exists)
                    di.Create();
                string caminho = DateTime.Now.ToString() + fileArquivo.FileName;
                caminhosalvar = caminho.Replace("/", "").Replace(":", "").Replace(" ", "");
                caminho = di.FullName + "\\" + caminhosalvar;
                fileArquivo.SaveAs(caminho);


                //TIPO
                string extensao = System.IO.Path.GetExtension(this.fileArquivo.FileName);
                if (extensao == ".jpg" || extensao == ".jpeg" || extensao == ".png" || extensao == ".gif" || extensao == ".bmp")
                {
                    tipo = 1;
                }
                if (extensao == ".mp4" || extensao == ".3gp" || extensao == ".avi" || extensao == ".wmv" || extensao == ".mpeg" || extensao == ".mpg" || extensao == ".webm" || extensao == ".ogg")
                {
                    tipo = 2;
                }


            }
            catch { }



            //SALVA NO BANCO
            dsTableAdapters.midiaTableAdapter ta = new dsTableAdapters.midiaTableAdapter();
            if (fileArquivo.FileName == "")
            {
                ta.Insert(DateTime.Now, "", 3, txtObservacao.Text);
            }
            else
            {
                ta.Insert(DateTime.Now, Properties.Resources.ServerPath + caminhosalvar, tipo, txtObservacao.Text);
            }


            dsTableAdapters.midia_pacienteTableAdapter tapac = new dsTableAdapters.midia_pacienteTableAdapter();
            tapac.Insert(WS.UltimaMidia(), int.Parse(Session["paciente"].ToString()));

            dsTableAdapters.midia_profissionalTableAdapter tapro = new dsTableAdapters.midia_profissionalTableAdapter();
            tapro.Insert(WS.UltimaMidia(), 10);//HARDCODE



            //RESPONSE
            Response.Redirect("TabletHistorico.aspx?paciente=" + Session["paciente"].ToString());

        }
    }
}